<template>
  <view class="container" v-if="goods_info.goods_name">
    <!-- 轮播图 -->
    <view class="swiper">
      <swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" circular>
        <swiper-item v-for="(item,index) in goods_info.pics" :key="item.pics_id">
          <view class="swiper-item" @click="preiveImg(index)">
            <image :src="item.pics_big" ></image>
          </view>
        </swiper-item>
      </swiper>
    </view>
    <!-- 商品信息 -->
    <view class="goods-info">
      <!-- 价钱 -->
      <view class="price">
        ￥{{ goods_info.goods_price }}
      </view>
      <!-- 标题 -->
      <view class="title-body">
        <view class="title">{{ goods_info.goods_name }} </view>
        <view class="hot"> 
        <uni-icons  color="#999" type="star" size="25"></uni-icons>
<!--        <uni-icons  color="#c00000" type="star-filled" size="25"></uni-icons> -->
        <view>收藏</view>
        </view>
      </view>
      <view class="freight">快递：免运费</view>
    </view>
    <!-- 商品描述 -->
    <rich-text :nodes="goods_info.goods_introduce"></rich-text>
    <!-- 底部工具栏 -->
    <view class="footer">
    	<uni-goods-nav :fill="true"  :options="options" :buttonGroup="buttonGroup"  @click="onClick" @buttonClick="buttonClick" />
    </view>
  </view>
</template>

<script>
  import { getDetail } from '@/api/goodsdetail.js'
  import { mapState,mapMutations,mapGetters } from 'vuex'
  export default {
    data() {
      return {
        goods_info:{},
          options: [
            {
        			icon: 'shop',
        			text: '店铺',
        			// info: 2,
        			// infoBackgroundColor:'#007aff',
        			// infoColor:"red"
        		}, {
        			icon: 'cart',
        			text: '购物车',
        			info: 0
        		}],
        	    buttonGroup: [{
        	      text: '加入购物车',
        	      backgroundColor: '#ff0000',
        	      color: '#fff'
        	    },
        	    {
        	      text: '立即购买',
        	      backgroundColor: '#ffa200',
        	      color: '#fff'
        	    }
        	    ]
      };
    },
    onLoad(options) {
      const goods_id = options.goods_id
      // console.log(goods_id);
      this.getGoodsDetail(goods_id)
    },
    computed:{
      ...mapState('cart',[]),
      ...mapGetters(['total'])
    },
    watch:{
      total:{
        handler(newCount) {
          this.options[1].info = newCount
        },
        deep:true,
        immediate:true
      }
    },
    methods:{
      ...mapMutations('cart',['ADDCART']),
     async getGoodsDetail(id) {
        const { data } = await getDetail(id)
        if(data.meta.status !== 200) return uni.$showMsg()
        data.message.goods_introduce = data.message.goods_introduce.replace(/<img /g,'<img style="display:block;" ').replace(/webp/g,'jpg')
        this.goods_info = data.message
        // console.log(data);
      },
      // 预览图片
      preiveImg(index) {
        uni.previewImage({
          current:index,
          urls:this.goods_info.pics.map(item => item.pics_big)
        })
      },
      onClick (e) {
      // console.log(e.content.text);
      if(e.content.text === '购物车') {
        uni.switchTab({
          url:'../../pages/cart/cart'
        })
      }
      },
      buttonClick (e) {
        // console.log(e)
        // this.options[1].info++
        if(e.content.text === '加入购物车') {
          // 点击加入购物车
          // 组织商品信息对象
          const goods = {
            goods_id:this.goods_info.goods_id,
            goods_name:this.goods_info.goods_name,
            goods_price:this.goods_info.goods_price,
            goods_count:1,
            goods_small_logo:this.goods_info.goods_small_logo,
            goods_state:true
          }
          // 调用ADDCART方法
          this.ADDCART(goods)
        }
      },
    }
  }
</script>

<style scoped lang="scss">
  .container {
    padding-bottom: 50px;
  }
swiper {
  height: 750rpx;
.swiper-item {
    width: 100%;
    height: 100%;
    image {
      width: 100%;
      height: 100%;
    }
  }
}
.goods-info {
  padding: 15rpx;
  .price {
    color: #c00000;
    font-weight: 700;
    margin: 15rpx;
    font-size: 18px;
  }
  .title-body {
    display: flex;
    justify-content: space-between;
    .hot {
      // flex: 1;
      display: flex;
      justify-content: center;
      align-items: center;
      flex-direction: column;
      width: 150px;
      height: 100%;
      color: #999;
    }
  }
  .freight {
    font-size: 14px;
    color: #888;
    margin: 10rpx 0;
  }
}
.footer {
  width: 100%;
  position: fixed;
  bottom: 0%;
  left: 0;
}
</style>
